package com.ruoyi.user.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.user.domain.YmTypeEntity;
import com.ruoyi.user.enums.ReqHandleExceptionEnum;
import com.ruoyi.user.pojo.ReqHandleVo;
import com.ruoyi.user.pojo.YmNaichaVo;
import com.ruoyi.user.service.IYmTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.YmProductEntity;
import com.ruoyi.user.service.IYmProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品信息 Controller
 * 
 * @author zq
 * @date 2022-11-08
 */
@RestController
@RequestMapping("/ymapp/product")
@Slf4j
public class YmProductController extends BaseController
{
    @Autowired
    private IYmProductService ymProductService;
    @Autowired
    private IYmTypeService ymTypeService;

    /**
     * 根据产品的类型id，查询对应的奶茶信息
     */
//    @PreAuthorize("@ss.hasPermi('myapp:product:queryProductByType')")
    @GetMapping("/queryProductByType")
    public ReqHandleVo<YmNaichaVo> queryProductByType(@RequestParam("id") Long typeId){
        ReqHandleVo<YmNaichaVo> reqHandleVo = new ReqHandleVo<>();

        // 获取对应的产品信息
        List<YmProductEntity> products = ymProductService.selectYmProductsByTypeId(typeId);
        if(CollectionUtils.isEmpty(products)){
            // 不存在，表示该类型的奶茶已购完
            reqHandleVo =  reqHandleVo.buildError(ReqHandleExceptionEnum.PRODUCT_IS_EMPTY.getDesc());
//            log.info("ReqHandleVo：{}",reqHandleVo);
            return reqHandleVo;
        }

        // 获取对应的产品类型信息
        YmTypeEntity ymTypeEntity = ymTypeService.selectYmTypeById(typeId);
        List<YmNaichaVo> ymNaichaVos = products.stream().map(ymProductEntity -> {
            YmNaichaVo ymNaichaVo = new YmNaichaVo();
            BeanUtils.copyProperties(ymProductEntity, ymNaichaVo);
            ymNaichaVo.setType(ymTypeEntity);
            return ymNaichaVo;
        }).collect(Collectors.toList());

        reqHandleVo.setData(ymNaichaVos);
        reqHandleVo =  reqHandleVo.buildSuccess("请求成功");
//        log.info("ReqHandleVo：{}",reqHandleVo);
        return reqHandleVo;
    }


    /**
     * 查询列表
     */
//    @PreAuthorize("@ss.hasPermi('myapp:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(YmProductEntity ymProductEntity)
    {
        startPage();
        List<YmProductEntity> list = ymProductService.selectYmProductList(ymProductEntity);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
//    @PreAuthorize("@ss.hasPermi('myapp:product:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YmProductEntity ymProductEntity)
    {
        List<YmProductEntity> list = ymProductService.selectYmProductList(ymProductEntity);
        ExcelUtil<YmProductEntity> util = new ExcelUtil<YmProductEntity>(YmProductEntity.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('myapp:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ymProductService.selectYmProductById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('myapp:product:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YmProductEntity ymProductEntity)
    {
        return toAjax(ymProductService.insertYmProduct(ymProductEntity));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('myapp:product:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YmProductEntity ymProductEntity)
    {
        return toAjax(ymProductService.updateYmProduct(ymProductEntity));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('myapp:product:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ymProductService.deleteYmProductByIds(ids));
    }
}
